package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.CarData;
import com.rain.shoppingscienceproj.module.ShoppingcarDto;
import com.rain.shoppingscienceproj.utils.ImageUtils;
import com.rain.shoppingscienceproj.utils.layoutmanager.CustomLinearLayoutManager;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/9/3 0:07
 */
public class MakeOrderAdapter extends RecyclerView.Adapter<MakeOrderAdapter.MyViewHolder> {

    private Context mContext;
    private List<CarData> list;
    DecimalFormat df = new DecimalFormat("#.00");
    private int type;//1用戶  2商戶

    private CancelButtonListener cancelButtonListener;

    private SubmitButtonListener submitButtonListeners;


    public void setCancelButtonListener(CancelButtonListener cancelButtonListener) {
        this.cancelButtonListener = cancelButtonListener;
    }

    public void setSubmitButtonListener(SubmitButtonListener submitButtonListener) {
        this.submitButtonListeners = submitButtonListener;
    }

    public void setList(List<CarData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public MakeOrderAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_make, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        CarData myCarData = list.get(position);
        if (null != myCarData.getShop().getShopImage() || !"".equals(myCarData.getShop().getShopImage())) {
            ImageUtils.imageShop(mContext, myCarData.getShop().getShopImage(), holder.iv_store_image);
        }
        double money = 0.00;
        for (ShoppingcarDto goods :
                myCarData.getList()) {
            if (goods.getGoodsPay() != null && goods.getSize() != null) {
                money += (Double.valueOf(goods.getGoodsPay()) / 100) * goods.getSize();
            }
        }
        holder.tv_title.setText(myCarData.getShop().getShopName());
        CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(mContext);
        holder.rv_goods_order.setLayoutManager(linearLayoutManager);
        OrderGoodsAdapter orderGoodsAdapter = new OrderGoodsAdapter(mContext);
        holder.rv_goods_order.setAdapter(orderGoodsAdapter);
        orderGoodsAdapter.setList(myCarData.getList());
        holder.tv_order_num.setText("共计" + list.get(position).getList().size() + "件商品  小计:");
        if (money < 1) {
            holder.tv_money.setText(0 + df.format(money));
        } else {
            holder.tv_money.setText(df.format(money));
        }
        holder.tv_cancel_order.setVisibility(View.VISIBLE);
        holder.ll_button.setVisibility(View.VISIBLE);
        holder.ll_button.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_layout)
        LinearLayout ll_layout;

        @BindView(R.id.iv_store_image)
        ImageView iv_store_image;

        @BindView(R.id.tv_title)
        TextView tv_title;

        @BindView(R.id.rv_goods_order)
        RecyclerView rv_goods_order;

        @BindView(R.id.tv_order_num)
        TextView tv_order_num;

        @BindView(R.id.tv_money)
        TextView tv_money;

        @BindView(R.id.ll_button)
        LinearLayout ll_button;

        @BindView(R.id.tv_cancel_order)
        TextView tv_cancel_order;

        @BindView(R.id.tv_submit_order)
        TextView tv_submit_order;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface ItemClickListener {
        void ItemClickListener(String orderId);
    }

    public interface CancelButtonListener {
        void CancelButtonClickListener(int type, String orderId);
    }

    public interface SubmitButtonListener {
        void submitButtonListener(int type, String orderId, String shopId);
    }
}
